package com.recSystem.Utils;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：cwyy
 * @Date：2023/4/23 12:41
 */
public class HttpUtils {
    /**
     * 以post或get方式调用对方接口方法，
     * @param param
     */
    public  String doPostOrGet(JSONObject param){
        OutputStreamWriter out = null;
        BufferedReader br = null;
        String result = "";
//        List<String> result = new ArrayList<>();

        // 设置IP代理 演示时候直连可以注释掉 如果走代理不能注释
//        System.setProperty("http.proxyHost", "116.204.79.186");
//        System.setProperty("http.proxyPort", "8888");
//        System.setProperty("http.proxyHost", "39.144.59.74");
        try {
            URL url = new URL("http://219.216.64.42:4780/rec");
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //请求方式
            conn.setRequestMethod("POST");
            //conn.setRequestMethod("GET");

            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            //DoOutput设置是否向httpUrlConnection输出，DoInput设置是否从httpUrlConnection读入，此外发送post请求必须设置这两个
            conn.setDoOutput(true);
            conn.setDoInput(true);

            /**
             * 下面的三句代码，就是调用第三方http接口
             */
            //获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            //发送请求参数即数据
            out.write(param.toString());
//            System.out.println(param);
            //flush输出流的缓冲
            out.flush();

            /**
             * 下面的代码相当于，获取调用第三方http接口后返回的结果
             */
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            br = new BufferedReader(new InputStreamReader(is));
//            List<String> stringList = new ArrayList<>();
            String str = "";
//            System.out.println(br.readLine());
//            System.out.println(11111);
            while ((str = br.readLine()) != null){
                result += str;
            }
            System.out.println(result);
            //关闭流
            is.close();
            //断开连接，disconnect是在底层tcp socket链接空闲时才切断，如果正在被其他线程使用就不切断。
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (out != null){
                    out.close();
                }
                if (br != null){
                    br.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
